home *** CD-ROM | disk | FTP | other *** search
- /*
- ************************************************************************
- * Handling of Mandatory Apple Events
- * OpenApplication, {Open|Print}Document, Quit
- */
-
-
- class AppleEventGenericHandler
- {
- const int signature;
- enum { Signature_value = 57049 };
-
- AppleEvent * theAppleEvent; // The event we got
- AppleEvent * reply; // Descriptor to add replies to
- virtual OSErr operator() (void) = 0; // This is the real handler
- AEEventHandlerUPP zero_level_handler_descr; // Just to tell the system if it's a native handler
- static pascal OSErr zero_level_handler(AppleEvent *theAppleEvent,
- AppleEvent *reply, long handlerRefcon);
- protected:
- const AEEventClass event_class;
- const AEEventID event_id; // Event we're handling
-
- Boolean got_all_params(void); // Check to make sure we got all required params
- void get_direct_list(AEDescList& list_desc); // Get Descriptor of a list as a direct param
-
- public:
- AppleEventGenericHandler(const AEEventID _event_id); // Install the handler
- ~AppleEventGenericHandler(void); // Uninstalls the handler
- };
-
- class AppleEventOpenAppl : public AppleEventGenericHandler
- {
- public:
- AppleEventOpenAppl(void) : AppleEventGenericHandler(kAEOpenApplication) {}
- };
-
- class QuitFlag : public AppleEventGenericHandler
- {
- Boolean quit_flag;
- virtual OSErr operator() (void) { quit_flag = TRUE; return noErr; }
- public:
- QuitFlag(void) : AppleEventGenericHandler(kAEQuitApplication), quit_flag(FALSE) {}
- Boolean quitting(void) const { return quit_flag; }
- };
-
- // Get a full path from an alias record
- // Returns a pointer to a static string
- const char * get_full_path(AliasHandle alias_handle);
-
- // Handle an OpenDocument AEvent with the help
- // of a plug-in class DocHandler
- // class DocHandler {
- // public: DocHandler(const int no_docs_to_open);
- // // throw up if it doesn't like this
- // // this number
- // Boolean operator () (AliasHandle doc_handle);
- // // Open the named document. Return
- // // FALSE if doesn't want to handle
- // // the rest
- template <class DocHandler>
- class AppleEventOpenDoc : public AppleEventGenericHandler
- {
- AEDescList desc_opened_doc; // Decsriptor of a list of open doc
- long no_opened_doc; // Number of documents to open
-
- virtual OSErr operator() (void)
- // This is the real handler
- // Sorry, this has to be inline
- // (CW6 sucks a bit on templates)
- {
- get_direct_list(desc_opened_doc);
- assert( got_all_params() );
- do_well( AECountItems(&desc_opened_doc, &no_opened_doc) );
- OSErr err_sofar = noErr;
- DocHandler doc_handler(no_opened_doc);
- int want_handle = 1; // Should've asked DocHandler abou that
- assert( want_handle <= no_opened_doc );
-
- const int alias_record_prealloc = 800; // To preallocate storage to copy
- // an alias record to (from AEDescr)
- // Hope that'll be enough
- AliasHandle alias_handle = (AliasHandle)NewHandle(alias_record_prealloc);
- for(register int index=1; err_sofar == noErr && index <= want_handle; index++)
- {
- Size actual_size;
- AEKeyword keyword;
- DescType type_code;
-
- HLock((Handle)alias_handle);
- do_well( AEGetNthPtr(&desc_opened_doc, index, typeAlias, &keyword, &type_code,
- (Ptr)*alias_handle, alias_record_prealloc, &actual_size) );
- assert( actual_size < alias_record_prealloc ); // Make sure we got the whole
- assert( actual_size == (**alias_handle).aliasSize ); // record
- HUnlock((Handle)alias_handle);
-
- // Note, returned alias is a minimal alias
- FSSpec resolved_target; // we make it a full one (via UpdateAlias)
- Boolean was_changed;
- do_well( ResolveAlias(0,alias_handle,&resolved_target,&was_changed) );
- do_well( UpdateAlias(0,&resolved_target,alias_handle,&was_changed) );
- if( !doc_handler(alias_handle) )
- err_sofar = errAEEventNotHandled;
- }
- do_well( AEDisposeDesc(&desc_opened_doc) );
- DisposeHandle((Handle)alias_handle);
-
- return err_sofar;
- }
- public:
- AppleEventOpenDoc(void) : AppleEventGenericHandler(kAEOpenDocuments) {}
- };
-
-